# https://leetcode.cn/problems/edit-distance/
# 72.编辑距离
# 给你两个单词word1和word2， 请返回将word1转换成word2所使用的最少操作数  。
# 你可以对一个单词进行如下三种操作：
# 插入一个字符
# 删除一个字符
# 替换一个字符

class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        dp = []
        for i in range(0, len(word1) + 1):
            dp.append([0] * (len(word2) + 1))
        for i in range(1, len(word1) + 1):
            dp[i][0] = dp[i - 1][0] + 1
        for j in range(1, len(word2) + 1):
            dp[0][j] = dp[0][j - 1] + 1

        for i in range(1, len(word1) + 1):
            for j in range(1, len(word2) + 1):
                if word1[i - 1] == word2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1]
                    continue

                dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1)
                dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1)

        return dp[len(word1)][len(word2)]

# print(Solution().minDistance("12312", "123123"))
